<?php

//评论控制器
class Comment extends Controller {

	var $userid;
	var $userinfo;
	var $siteid;
	
	function Comment()
	{
		parent::Controller();	
		$this->load->model("accounts_model", "am");
		$this->load->model("site_model", "sm");
		$this->load->model("plugins_model", "pm");
		$this->load->model("comment_model", "ctm");
		
		$this->siteid = u(3);
		//检测登陆状态
		$this->userid = $this->am->is_logined();
		//获取小站信息
		$this->site = $this->sm->get_site($this->siteid);
		//获取站长信息
		$this->owner = $this->am->get($this->site["userid"]);
		//检测主人
		$this->isowner = $this->is_owner();
		if ($this->userid) {
			$this->userinfo = $this->am->get($this->userid);
		}
	}
	
	//小站评论
	/*调用代码：<script language="javascript" src="<?=gurl('/');?>comment/index/<?=$site['id'];?>/<?=$plid;?>/<?=$id;?>/"></script> */
	function index()
	{
		//参数处理
		if(!$dataid = u(5)) {
			show_failed("非法请求");
		}
		$plid = u(4);
		$offset = u(6);
		$per_page = 30;
		
		//获取评论列表
		$lists = $this->ctm->lists($this->siteid, $plid, $dataid, $per_page, $offset);
		$r = $this->pm->get($plid);
		$moduleid = $r["moduleid"];

		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/show/'.$moduleid.'/'.$dataid;
		$total_rows = $this->ctm->count_lists($this->siteid, $plid, $dataid);
		$pagestring = pages($base_url,$total_rows,6,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		ob_start();
		$viewdata["lists"] = $lists;
		$viewdata["isowner"] = $this->isowner;
		$viewdata["userid"] = $this->userid;
		$this->load->view(__CLASS__."/index", $viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//添加评论
	function add()
	{
		if($dosubmit = p("dosubmit")) {
			if($this->userid == 0) {
				show_failed("非法请求");
			}
			$result = $this->ctm->add();
			if ($result) {
				//添加回应提醒
				$plugin = $this->pm->get(p("plid"));
				$moduleid = $plugin["moduleid"];
				$module = $this->pm->get_module($moduleid);
				$modulename = $module["modulename"];
				$plugmodel = $modulename."_model";
				$this->load->model($plugmodel);//加载
				$row = $this->$plugmodel->get_info(p("dataid"));
				$site = $this->sm->get_site(p("siteid"));
				if ($moduleid == 8) $row = $row[0];
				
				if ($moduleid == 8) {//相册
					$title = '<a href="/site/'.$site['domain'].'/show/'.$moduleid.'/'.p("dataid").'">'.$plugin['plname'].'的图片</a>';
				} else {
					$title = '<a href="/site/'.$site['domain'].'/show/'.$moduleid.'/'.p("dataid").'">'.$row['title'].'</a>';
				}
				if($row["userid"] != $this->userid) {//屏蔽回应自己提醒
					$this->ctm->add_message($row["userid"], $title);	
				}			
				if(p("rpid") && p("rpid") > 0 && $row["userid"] != p("rpid")) {
					$this->ctm->add_message(p("rpid"), $title);
				}
				comment_success("添加成功");
			}
		}
	}
	
	//删除评论
	function delete()
	{
		//参数处理
		$dataid = u(4);
		
		$row = $this->ctm->get($dataid);

		//安全检测
		if (!$this->owner && $row["userid"] != $this->userid) {
			show_failed("非法请求");
		}
		
		$result = $this->ctm->delete($dataid);
		if ($result) {
			show_success("删除成功");
		} 
	}
	
	//添加社区评论
	function forum_add()
	{
		if($dosubmit = p("dosubmit")) {
			if($this->userid == 0) {
				show_failed("非法请求");
			}
			//验证图片
			$pics = get_content_pics(p('content'));
			if (count($pics)>1) {
				show_failed("只能添加一张网络图片");
			}
			
			$result = $this->ctm->forum_add();
			if ($result) {
				//添加回应提醒
				$this->load->model("forum_model", "fm");
				$row = $this->fm->show(p("dataid"));
				$site = $this->sm->get_site(p("siteid"));
				$title = '<a href="/site/'.$site['domain'].'/forum/show/'.p("dataid").'">'.$row['title'].'</a>';
				if($row["userid"] != $this->userid) {//屏蔽回应自己提醒
					$this->ctm->add_message($row["userid"], $title);	
				}
				if(p("rpid") && p("rpid") > 0 && $row["userid"] != p("rpid")) {
					$this->ctm->add_message(p("rpid"), $title);
				}
				comment_success("添加成功");
			}
		}
	}
	
	//删除社区评论
	function forum_delete()
	{
		//参数处理
		$dataid = u(4);
		
		$row = $this->ctm->forum_get($dataid);

		//安全检测
		if (!$this->owner && $row["userid"] != $this->userid) {
			show_failed("非法请求");
		}
		
		$result = $this->ctm->forum_delete($dataid);
		if ($result) {
			show_success("删除成功");
		} 
	}
	
	//社区评论
	/*调用代码：<script language="javascript" src="<?=gurl('/');?>comment/forum/<?=$site['id'];?>/<?=$row['id'];?>/"></script> */
	function forum()
	{
		//参数处理
		if(!$dataid = u(4)) {
			show_failed("非法请求");
		}
		
		$offset = u(5);
		$per_page = 30;
		
		//获取评论列表
		$lists = $this->ctm->forum_lists($this->siteid, $dataid, $per_page, $offset);
		
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/forum/show/'.$dataid;
		$total_rows = $this->ctm->count_forum_lists($this->siteid, $dataid);
		$pagestring = pages($base_url,$total_rows,5,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		
		ob_start();
		$viewdata["lists"] = $lists;
		$viewdata["siteid"] = $this->siteid;
		$viewdata["id"] = $dataid;
		$viewdata["isowner"] = $this->isowner;
		$viewdata["userid"] = $this->userid;
		$this->load->view(__CLASS__."/forum", $viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//添加广播评论
	function miniblog_add()
	{
		if($dosubmit = p("dosubmit")) {
			if($this->userid == 0) {
				show_failed("非法请求");
			}
			$result = $this->ctm->miniblog_add();
			if ($result) {
				/*//添加回应提醒
				$site = $this->sm->get_site(p("siteid"));
				$this->load->model("miniblog_model", "mbm");
				$row = $this->mbm->get($site["id"]);
				if($row && $site["userid"] != $this->userid) {//屏蔽回应自己提醒
					$title = '<a href="/site/'.$site['domain'].'/miniblog">'.msubstr($row['description'],0,100).'</a>';
					$this->ctm->add_message($site["userid"], $title);
				}*/
				show_success("添加成功");
			}
		}
	}
	
	//广播评论
	/*调用代码：<script language="javascript" src="<?=gurl('/');?>comment/miniblog/<?=$site['id'];?>/<?=$row['id'];?>/"></script> */
	function miniblog()
	{
		//参数处理
		if(!$dataid = u(4)) {
			show_failed("非法请求");
		}
		
		$offset = u(5);
		$per_page = 40;
		
		//获取评论列表
		$lists = $this->ctm->miniblog_lists($this->siteid, $dataid, $per_page, $offset);
		
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/miniblog/';
		$total_rows = $this->ctm->count_miniblog_lists($this->siteid, $dataid);
		$pagestring = pages($base_url,$total_rows,5,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		
		ob_start();
		$viewdata["lists"] = $lists;
		$viewdata["siteid"] = $this->siteid;
		$viewdata["id"] = $dataid;
		$viewdata["userid"] = $this->userid;
		$this->load->view(__CLASS__."/miniblog", $viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//添加专题评论
	function special_add()
	{
		if($dosubmit = p("dosubmit")) {
			if($this->userid == 0) {
				show_failed("非法请求");
			}
			$result = $this->ctm->special_add();
			if ($result) {
				show_success("添加成功");
			}
		}
	}
	
	//删除专题评论
	function special_delete()
	{
		//参数处理
		$dataid = u(4);
		
		$row = $this->ctm->special_get($dataid);

		//安全检测
		if (!$this->owner && $row["userid"] != $this->userid) {
			show_failed("非法请求");
		}
		
		$result = $this->ctm->special_delete($dataid);
		if ($result) {
			show_success("删除成功");
		} 
	}
	
	//专题评论
	/*调用代码：<script language="javascript" src="<?=gurl('/');?>comment/miniblog/<?=$site['id'];?>/<?=$row['id'];?>/"></script> */
	function special()
	{
		//参数处理
		if(!$dataid = u(5)) {
			show_failed("非法请求");
		}
		$plid = u(4);
		$offset = u(6);
		$per_page = 30;
		
		//获取评论列表
		$lists1 = $this->ctm->special_lists($this->siteid, $plid, $dataid, $per_page, $offset);
		$lists2 = $this->ctm->special_lists($this->siteid, $plid, $dataid, $per_page, $offset, 1);
		$r = $this->pm->get($plid);
		$moduleid = $r["moduleid"];

		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/show/'.$moduleid.'/'.$dataid;
		$total_rows = $this->ctm->count_special_lists($this->siteid, $plid, $dataid);
		$pagestring = pages($base_url,$total_rows,6,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		
		ob_start();
		$viewdata["lists1"] = $lists1;
		$viewdata["lists2"] = $lists2;
		$viewdata["count1"] = $this->ctm->count_special_lists1($this->siteid, $plid, $dataid);
		$viewdata["count2"] = $this->ctm->count_special_lists2($this->siteid, $plid, $dataid);
		$viewdata["isowner"] = $this->isowner;
		$viewdata["userid"] = $this->userid;
		$viewdata["plid"] = $plid;
		$viewdata["siteid"] = $this->siteid;
		$viewdata["id"] = $dataid;
		$this->load->view(__CLASS__."/special", $viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//检测是否是小站主人
	function is_owner()
	{
		if($this->userid > 0 && $this->userid == $this->owner["id"]) return true;
		return false;
	}
	
}

/* End of file */